package login;

import dbhandler.Login;
import evthandler.EventMessage;
import encryption.SHA1;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class LoginCheck {

    private Login login;

    /**
     *
     */
    public LoginCheck() {
    }

    /**
     * Method for checking if typed login is valid and returns a boolean as the result.
     * @param userName Username to check
     * @param charPass Password to check
     * @return Returns true if the encoded password and the stored password match\n otherwise an error message is shown
     */
    public boolean validLogin(String userName, char[] charPass) {
        SHA1 encryption = new SHA1(charPass);
        String encodedPass = encryption.getEncodedPassword();
        boolean loggedIn = false;
        if (new Login().exists(userName)) {
            login = new Login(userName);
            String userPass = login.getUserPass();
            if (userPass.equalsIgnoreCase(encodedPass) && !userPass.equals("") && !encodedPass.equals("")) {
                loggedIn = true;
                new EventMessage().writeLogin(userName);
            }
        }
        return loggedIn;
    }
}